import OperatorCode from './operatorCode.js'
import opConfig from './operatorConfig.js'
import Log from '../util/log.js'

// Return results
const SUCCESS = 200;
const SUCCESS_MESSAGE = "Success";
const ERROR = 201;
const ERROR_MESSAGE = "Service not defined";

const TAG = 'JS/Operator/Common: '

var getRequestAction = function (requestCode) {
    return {
        bundleName: opConfig.OP_BUNDLE_NAME,
        abilityName: opConfig.COMMON_ABILITY_NAME,
        abilityType: OperatorCode.ABILITY_TYPE_INTERNAL,
        syncOption: OperatorCode.ACTION_SYNC,
        messageCode: requestCode,
    };
};

export default {
    data: {},
    checkPermission: async function (permissions, callback) {
        let actionData = {};
        actionData.permissions = permissions;

        let action = getRequestAction(OperatorCode.CHECK_PERMISSION);
        action.data = actionData;

        let resultStr = await FeatureAbility.subscribeAbilityEvent(action, callbackData => {
            let callbackJson = JSON.parse(callbackData);
            callback(callbackJson);
        });

        Log.info(TAG + 'Check Permission result is:' + resultStr);
        let resultObj = JSON.parse(resultStr);
        return resultObj;
    },
    requestPermission: async function (permissions, requestCode, callback) {
        let actionData = {};
        actionData.permissions = permissions;
        actionData.requestCode = requestCode;

        let action = getRequestAction(OperatorCode.REQUEST_PERMISSION);
        action.data = actionData;

        let resultStr = await FeatureAbility.subscribeAbilityEvent(action, callbackData => {
            let callbackJson = JSON.parse(callbackData);
            callback(callbackJson);
        });

        Log.info(TAG + 'Request Permission result is:' + resultStr);
        let resultObj = JSON.parse(resultStr);
        return resultObj;
    },
    startAbility: async function (bundleName, abilityName, callback, params, options) {
        let actionData = {};
        actionData.bundleName = bundleName;
        actionData.abilityName = abilityName;
        actionData.params = params;
        actionData.options = options;

        let action = getRequestAction(OperatorCode.START_ABILITY);
        action.data = actionData;

        let resultStr = await FeatureAbility.subscribeAbilityEvent(action, callbackData => {
            let callbackJson = JSON.parse(callbackData);
            callback(callbackJson);
        });

        Log.info(TAG + 'Start Ability result is:' + resultStr);
        let resultObj = JSON.parse(resultStr);
        return resultObj;
    },
    checkPackageInstallation: async function (bundleName, callback) {
        let actionData = {};
        actionData.bundleName = bundleName;

        let action = getRequestAction(OperatorCode.CHECK_PACKAGE_INSTALLATION);
        action.data = actionData;

        let resultStr = await FeatureAbility.subscribeAbilityEvent(action, callbackData => {
            let callbackJson = JSON.parse(callbackData);
            callback(callbackJson);
        });

        Log.info(TAG + 'Check Package Installation result is:' + resultStr);
        let resultObj = JSON.parse(resultStr);
        return resultObj;
    },
    openUrl: async function (url, callback) {
        let actionData = {};
        actionData.url = url;

        if (actionData.url == null || actionData.url == "") {
            Log.info(TAG + 'actionData.url is empty or null:' + JSON.stringify(actionData.url));
            return;
        }
        let action = getRequestAction(OperatorCode.OPEN_URL);
        action.data = actionData;

        let resultStr = await FeatureAbility.subscribeAbilityEvent(action, callbackData => {
            let callbackJson = JSON.parse(callbackData);
            callback(callbackJson);
        });
        let resultObj = JSON.parse(resultStr);
        Log.info(TAG + 'OpenUrl result is:' + JSON.stringify(resultObj));
    },
    getUniqueId: async function (callback) {
        Log.info(TAG + 'get uniqueId');
        let action = getRequestAction(OperatorCode.GET_UNIQUE_ID);

        let resultStr = await FeatureAbility.subscribeAbilityEvent(action, callbackData => {
            try {
                let callbackJson = JSON.parse(callbackData);
                callback(callbackJson);
            } catch (e) {
                Log.error(TAG + "callbackJson get unique e =" + e);
            }
        });
        Log.info(TAG + 'get uniqueId result is:' + resultStr);
    },
    startSystemAbility: async function (bundleName, abilityName, callback, params, options) {
        let actionData = {};
        actionData.bundleName = bundleName;
        actionData.abilityName = abilityName;
        actionData.params = params;
        actionData.options = options;

        let action = getRequestAction(OperatorCode.START_SYSTEM_ABILITY);
        action.data = actionData;

        let resultStr = await FeatureAbility.subscribeAbilityEvent(action, callbackData => {
            let callbackJson = JSON.parse(callbackData);
            callback(callbackJson);
        });

        console.debug(TAG + 'Start System Ability result is:' + resultStr);
        let resultObj = JSON.parse(resultStr);
        return resultObj;
    },

    // 查询版本信息
    checkDeviceVersion: async function (url, callback) {
        let actionData = {};
        actionData.url = url;
        console.debug(TAG + 'checkDeviceVersion  url:' + actionData.url);
        let action = getRequestAction(OperatorCode.GET_OTA_VERSION);
        action.data = actionData;
        let resultStr = await FeatureAbility.subscribeAbilityEvent(action, callbackData => {
            try {
                console.debug(TAG + 'resultStr' + callbackData)
                let callbackJson = JSON.parse(callbackData);
                callback(callbackJson);
            } catch (e) {
                console.error(TAG + "callbackJson get unique e =" + e);
            }
        });
        console.debug(TAG + 'result String' + resultStr);
    },
    // 下载固件包
    downloadFirmware: async function (url, callback) {
        let actionData = {};
        actionData.url = url;
        console.debug(TAG + 'downloadFirmware  url:' + actionData.url);
        let action = getRequestAction(OperatorCode.DOWNLOAD_OTA_FILE);
        action.data = actionData;
        let resultStr = await FeatureAbility.subscribeAbilityEvent(action, callbackData => {
            try {
                console.debug(TAG + 'downloadFirmware result' + callbackData)
                let callbackJson = JSON.parse(callbackData);
                callback(callbackJson);
            } catch (e) {
                console.error(TAG + "callbackJson get downloadFirmware e =" + e);
            }
        });
        console.debug(TAG + 'downloadFirmware result String' + resultStr);
    },
    // 获取本地固件包16进制字节码流
    getFirmwareData: async function (callback) {
        let action = getRequestAction(OperatorCode.GET_OTA_FILE);
        let resultStr = await FeatureAbility.subscribeAbilityEvent(action, callbackData => {
            try {
                let callbackJson = JSON.parse(callbackData);
                callback(callbackJson);
            } catch (e) {
                console.error(TAG + "callbackJson get getFirmwareData e =" + e);
            }
        });
        console.debug(TAG + 'getFirmwareData result String' + resultStr);
    },
}